package cn.org.rookie.jeesdp.system.entity;

import cn.org.rookie.jeesdp.core.BaseEntity;
import cn.org.rookie.jeesdp.mapper.annotation.Table;
import cn.org.rookie.jeesdp.mapper.annotation.Transient;
import lombok.Builder;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.ToString;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

@Data
@Builder
@Table("sys_menu")
@ToString(callSuper = true)
@EqualsAndHashCode(callSuper = false)
public class Menu extends BaseEntity {

    private String label;
    private String name;
    private String path;
    private String icon;
    private String pid;
    @Transient
    private String description;
    @Transient
    private String permission;
    private String component;
    @Transient
    private List<Menu> children;

    public Menu filtrationByPermission(Collection<String> permissions) {
        for (String item : permissions) {
            if (item.equals(this.permission)) {
                List<Menu> menuList = new ArrayList<>();
                for (Menu menu : this.children) {
                    if (menu.filtrationByPermission(permissions) != null) {
                        menuList.add(menu);
                    }
                }
                children = menuList;
                return this;
            }
        }
        return null;
    }

}

